package com.lin.controller.user;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lin.common.constant.JwtClaimsConstant;
import com.lin.common.context.BaseContext;
import com.lin.common.properties.JwtProperties;
import com.lin.common.result.Result;
import com.lin.common.utils.JwtUtil;
import com.lin.entity.User;
import com.lin.entity.dto.UserDto;
import com.lin.entity.vo.UserVo;
import com.lin.service.LoginService;
import com.lin.service.PersonService;
import com.lin.service.RegisterService;
import com.lin.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
@Slf4j
public class UserController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private RegisterService registerService;

	@Autowired
	private JwtProperties jwtProperties;

	@Autowired
	private PersonService personService;
	/**
	 * 注册时发送验证码
	 * @param userDto
	 * @return
	 */

    @PostMapping("/send")
    @ApiOperation("发送验证码")
    public Result send(@RequestBody UserDto userDto) {
        log.info("发送验证码用户：{}", userDto);
        String token = registerService.send(userDto);
        return Result.success(token);
    }

	/**
	 * 注册
	 * @param userDto
	 * @return
	 */
	@PostMapping("/register")
	@ApiOperation("注册")
	public Result registerUser(@RequestBody UserDto userDto){
		log.info("注册用户：{}",userDto);
		registerService.registerUser(userDto);
		return Result.success();
	}

	/**
	 * 登录
	 * @param userDto
	 * @return
	 */
	@PostMapping("/login")
	@ApiOperation("登录")
	public Result<UserVo> loginUser(@RequestBody UserDto userDto) {
		log.info("用户登录：{}",userDto);
		//登录成功后，生成jwt令牌
		User user = loginService.loginUser(userDto);;
		Map<String, Object> claims = new HashMap<>();
		claims.put(JwtClaimsConstant.EMP_ID, user.getUserId());
		String token = JwtUtil.createJWT(
				jwtProperties.getAdminSecretKey(),
				jwtProperties.getAdminTtl(),
				claims);
		UserVo userVo = new UserVo();
		userVo.setToken(token);
		BaseContext.setCurrentId(user.getUserId());
		BeanUtils.copyProperties(user,userVo);
		return Result.success(userVo);
	}
	/**
	 * 关注某人
	 * @param
	 * @return
	 */
	@PostMapping("/followAUser")
	@ApiOperation("添加关注")
	public JsonResult<Integer> followAUser(Integer fanId, Integer followedId){
		JsonResult<Integer> json;
		try{personService.followAUser(fanId,followedId);
			json = new JsonResult<>("1","关注成功");
		}
		catch (DataAccessException e) {
			json = new JsonResult<>("0","已经关注过了");
		}
		return json;
	}

	/**
	 *
	 * @param fanId
	 * @param followedId
	 * @return
	 */
	@PostMapping("/cancelFollowAUser")
	@ApiOperation("取消关注")
	public  JsonResult<Integer> cancelFollowAUser(Integer fanId,Integer followedId){
		JsonResult<Integer> json;
		int res = personService.cancelFollowAUser(fanId,followedId);
		json = new JsonResult<>(Integer.toString(res),"取消关注成功");
		return json;
	}

	/**
	 * 查找用户的粉丝
	 * @param userId
	 * @param page
	 * @return
	 */
	@PostMapping("/findFan")
	@ApiOperation("查找用户的粉丝")
	public JsonResult<List<User>> findFan(Integer userId, Integer page) {
		PageHelper.startPage(page, 20);
		List<User> res =personService.findFan(userId);
		PageInfo<User> pageInfo = new PageInfo<>(res);
		JsonResult<List<User>> json;
		json = !res.isEmpty()
				? new JsonResult<>(res, "1", "查询成功", pageInfo.getPages())
				: new JsonResult<>(res, "0", "没得粉丝", pageInfo.getPages());
		return json;
	}

	@PostMapping("/findFollow")
	@ApiOperation("查找用户的关注")
	public JsonResult<List<User>> findFollow(Integer userId, Integer page) {
		PageHelper.startPage(page, 20);
		List<User> res =personService.findFollow(userId);
		PageInfo<User> pageInfo = new PageInfo<>(res);
		JsonResult<List<User>> json;
		json = !res.isEmpty()
				? new JsonResult<>(res, "1", "查询成功", pageInfo.getPages())
				: new JsonResult<>(res, "0", "没得关注", pageInfo.getPages());
		return json;
	}
	@GetMapping("/findFanNumById")
	@ApiOperation("查找用户的粉丝数量")
	public int getFanNumById(Integer userId){
		int fanNum = personService.getFanNumById(userId);
		return fanNum;
	}
	@GetMapping("/FollowNumById")
	@ApiOperation("查找用户的关注数量")
	public int getFollowNumById(Integer userId){
		int FollowNum = personService.getFollowNumById(userId);
		return FollowNum;
	}

	@GetMapping("/getdynamicNumById")
	@ApiOperation("查找用户的动态数量")
	public int getdynamicNumById(Integer userId){
		int dynamicNum = personService.getdynamicNumById(userId);
		return dynamicNum;
	}
}
